package repository

import (
	"gitee.com/ysl729895/go-mall/internal/model"
	"gorm.io/gorm"
)

// RoleRepository 角色数据操作接口
//go:generate mockgen -source=role_repository.go -destination=mock_role_repository.go -package=repository

type RoleRepository interface {
	Create(role *model.Role) error
	Update(role *model.Role) error
	Delete(id uint) error
	GetByID(id uint) (*model.Role, error)
	GetAll() ([]model.Role, error)
}

type roleRepository struct {
	db *gorm.DB
}

func NewRoleRepository(db *gorm.DB) RoleRepository {
	return &roleRepository{db: db}
}

func (r *roleRepository) Create(role *model.Role) error {
	return r.db.Create(role).Error
}

func (r *roleRepository) Update(role *model.Role) error {
	return r.db.Save(role).Error
}

func (r *roleRepository) Delete(id uint) error {
	return r.db.Delete(&model.Role{}, id).Error
}

func (r *roleRepository) GetByID(id uint) (*model.Role, error) {
	var role model.Role
	err := r.db.First(&role, id).Error
	return &role, err
}

func (r *roleRepository) GetAll() ([]model.Role, error) {
	var roles []model.Role
	err := r.db.Find(&roles).Error
	return roles, err
}
